<template>
    <!-- <span :style="{ width: size, height: size, color: color }" class="svg-icon">
      <svg v-if="modifiedSvgContent" v-html="modifiedSvgContent" preserveAspectRatio="xMidYMid meet"></svg>
    </span> -->
    <svg :style="{ width: size, height: size, color: color }" v-if="modifiedSvgContent" v-html="modifiedSvgContent" preserveAspectRatio="xMidYMid meet"></svg>
  </template>
  
  <script>
  export default {
    name: 'SvgIcon',
    props: {
      svgPath: {
        type: String,
        required: true
      },
      color: {
        type: String,
        default: '#000'
      },
      size: {
        type: String,
        default: '24px'
      }
    },
    data() {
      return {
        svgContent: '',
        modifiedSvgContent: ''
      };
    },
    mounted() {
      this.loadSvg();
    },
    methods: {
      async loadSvg() {
        try {
          let path = this.svgPath
          if (process.env.NODE_ENV === 'production') {
            path = `/h5${this.svgPath}`;
          }
          const response = await fetch(path);
          if (!response.ok) throw new Error('Failed to load SVG');
          this.svgContent = await response.text();
          this.modifySvgContent();
        } catch (error) {
          console.error(error);
        }
      },
      modifySvgContent() {
        // Create a DOM parser and parse the SVG content
        const parser = new DOMParser();
        const doc = parser.parseFromString(this.svgContent, 'image/svg+xml');
  
        // Select all path elements and set their fill attribute to currentColor
        const paths = doc.querySelectorAll('path');
        paths.forEach(path => {
          path.setAttribute('fill', 'currentColor');
        });
  
        // Set the viewBox attribute if not present
        const svgElement = doc.querySelector('svg');
        if (!svgElement.getAttribute('viewBox')) {
          svgElement.setAttribute('viewBox', '0 0 24 24'); // Adjust as necessary
        }
  
        // Serialize the modified SVG back to a string
        this.modifiedSvgContent = new XMLSerializer().serializeToString(doc);
      }
    }
  };
  </script>
  
  <style scoped>
  .svg-icon {
    display: inline-block;
    vertical-align: middle;
  }
  
  .svg-icon svg {
    width: 100%;
    height: 100%;
    fill: currentColor;
  }
  </style>
  
  
  
  